//
//  ZDQAddAddressViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZDQAddAddressViewController.h"
#import "ZDQCoreDataManager.h"
#import "AddressBook.h"

#define WIDTH self.view.frame.size.width
#define HEIFHT self.view.frame.size.height

@interface ZDQAddAddressViewController ()

@property(nonatomic, retain) UILabel *nameLabel;
@property(nonatomic, retain) UILabel *sexLabel;
@property(nonatomic, retain) UILabel *numberLabel;
@property(nonatomic, retain) UILabel *phoneNumberLabel;
@property(nonatomic, retain) UILabel *QQLabel;
@property(nonatomic, retain) UILabel *nativeLabel;
@property(nonatomic, retain) UITextField *nameTextField;
@property(nonatomic, retain) UITextField *sexTextField;
@property(nonatomic, retain) UITextField *numberTextField;
@property(nonatomic, retain) UITextField *phoneNumberTextField;
@property(nonatomic, retain) UITextField *QQTextField;
@property(nonatomic, retain) UITextField *nativeTextField;
@property(nonatomic, retain) ZDQCoreDataManager *manager;

@end

@implementation ZDQAddAddressViewController

- (void)dealloc {
    
    [_nameLabel release];
    [_sexLabel release];
    [_numberLabel release];
    [_phoneNumberLabel release];
    [_QQLabel release];
    [_nativeLabel release];
    [_nameTextField release];
    [_sexTextField release];
    [_numberTextField release];
    [_phoneNumberTextField release];
    [_QQTextField release];
    [_nativeTextField release];
    [_manager release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 40)];
    [self.view addSubview:self.nameLabel];
//    self.nameLabel.layer.borderWidth = 1;
    self.nameLabel.text = @"姓名:";
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [_nameLabel release];
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 25, 200, 30)];
    [self.view addSubview:self.nameTextField];
    self.nameTextField.layer.borderWidth = 1;
    self.nameTextField.layer.cornerRadius = 15;
    self.nameTextField.placeholder = @"请输入姓名";
    self.nameTextField.textAlignment = NSTextAlignmentCenter;
    self.nameTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_nameTextField release];
    
    self.sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 100, 40)];
    [self.view addSubview:self.sexLabel];
//    self.sexLabel.layer.borderWidth = 1;
    self.sexLabel.text = @"性别:";
    self.sexLabel.textAlignment = NSTextAlignmentCenter;
    [_sexLabel release];
    
    self.sexTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 75, 200, 30)];
    [self.view addSubview:self.sexTextField];
    self.sexTextField.layer.borderWidth = 1;
    self.sexTextField.layer.cornerRadius = 15;
    self.sexTextField.placeholder = @"请输入性别";
    self.sexTextField.textAlignment = NSTextAlignmentCenter;
    self.sexTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_sexTextField release];
    
    self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 100, 40)];
    [self.view addSubview:self.numberLabel];
//    self.numberLabel.layer.borderWidth = 1;
    self.numberLabel.text = @"学号:";
    self.numberLabel.textAlignment = NSTextAlignmentCenter;
    [_numberLabel release];
    
    self.numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 125, 200, 30)];
    [self.view addSubview:self.numberTextField];
    self.numberTextField.layer.borderWidth = 1;
    self.numberTextField.layer.cornerRadius = 15;
    self.numberTextField.placeholder = @"请输入学号";
    self.numberTextField.textAlignment = NSTextAlignmentCenter;
    self.numberTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_numberTextField release];


    self.phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 170, 100, 40)];
    [self.view addSubview:self.phoneNumberLabel];
//    self.phoneNumberLabel.layer.borderWidth = 1;
    self.phoneNumberLabel.text = @"电话号码:";
    self.phoneNumberLabel.textAlignment = NSTextAlignmentCenter;
    [_phoneNumberLabel release];
    
    self.phoneNumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 175, 200, 30)];
    [self.view addSubview:self.phoneNumberTextField];
    self.phoneNumberTextField.layer.borderWidth = 1;
    self.phoneNumberTextField.layer.cornerRadius = 15;
    self.phoneNumberTextField.placeholder = @"请输入电话号码";
    self.phoneNumberTextField.textAlignment = NSTextAlignmentCenter;
    self.phoneNumberTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_phoneNumberTextField release];

    
    self.QQLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 100, 40)];
    [self.view addSubview:self.QQLabel];
//    self.QQLabel.layer.borderWidth = 1;
    self.QQLabel.text = @"QQ:";
    self.QQLabel.textAlignment = NSTextAlignmentCenter;
    [_QQLabel release];
    
    self.QQTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 225, 200, 30)];
    [self.view addSubview:self.QQTextField];
    self.QQTextField.layer.borderWidth = 1;
    self.QQTextField.layer.cornerRadius = 15;
    self.QQTextField.placeholder = @"请输入QQ";
    self.QQTextField.textAlignment = NSTextAlignmentCenter;
    self.QQTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_QQTextField release];

    self.nativeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 270, 100, 40)];
    [self.view addSubview:self.nativeLabel];
//    self.nativeLabel.layer.borderWidth = 1;
    self.nativeLabel.text = @"籍贯:";
    self.nativeLabel.textAlignment = NSTextAlignmentCenter;
    [_nativeLabel release];
    
    self.nativeTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 275, 200, 30)];
    [self.view addSubview:self.nativeTextField];
    self.nativeTextField.layer.borderWidth = 1;
    self.nativeTextField.layer.cornerRadius = 15;
    self.nativeTextField.placeholder = @"请输入籍贯";
    self.nativeTextField.textAlignment = NSTextAlignmentCenter;
    self.nativeTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_nativeTextField release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    button.frame = CGRectMake(90, 350, 200, 40);
    [button setTitle:@"添加" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor lightGrayColor];
    button.layer.borderWidth = 1;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    

    //创建数据库管理器
    self.manager = [ZDQCoreDataManager defaults];
    NSLog(@"url ======= %@", [self.manager applicationDocumentsDirectory]);
    
}



- (void)buttonAction:(UIButton *)button {
    
//    //创建一个实体描述类
//    //参数1. 描述的实体
//    //参数2. 数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AddressBook" inManagedObjectContext:self.manager.managedObjectContext];
//
    //创建实体
    //参数1. 实体的描述
    //参数2. 数据管理器

    AddressBook *add = [[AddressBook alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    add.name = self.nameTextField.text;
    add.sex = self.sexTextField.text;
    add.number = self.numberTextField.text;
    add.phonenumber = self.phoneNumberTextField.text;
    add.qq = self.QQTextField.text;
    add.native = self.nativeTextField.text;
    
    if ([self.nameTextField.text isEqualToString:@""]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"姓名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:^{
            
            
        }];
    } else {
        
        //存储数据
        [self.manager saveContext];

        
    }
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
